﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;

namespace Wozuji.Logic
{
    public class groups
    {
        #region 添加组群
        public int createTeam(Wozuji.Model.Team_t t)
        {
            try
            {


                Wozuji.BLL.Team_t tmdal = new Wozuji.BLL.Team_t();
                //写入团队表
                int teamid = tmdal.Add(t);
                //写入inteam_t表
                Wozuji.Model.InTeam_t it = new Wozuji.Model.InTeam_t();
                Wozuji.BLL.InTeam_t itbll = new Wozuji.BLL.InTeam_t();

                it.InTeam_TeamId = teamid;
                it.InTeam_Time = DateTime.Now;
                it.InTeam_UserId = Convert.ToInt32(t.Team_Admin);
                itbll.Add(it);
                return teamid;
            }
            catch (Exception ex)
            {
                return 0;
            }


        }
        #endregion

        #region 组群是否存在
        public bool teamExist(String name)
        {
            Wozuji.BLL.Team_t tmdal = new Wozuji.BLL.Team_t();
            List<Wozuji.Model.Team_t> li = new List<Wozuji.Model.Team_t>();
            li = tmdal.GetModelList("team_name='" + name + "'");
            if (li.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 得到用户的组群信息

        public Wozuji.Model.Team_t getTeamById(int tid, int memberid)
        {
            //判断用户是否在这个gruop中
            Wozuji.BLL.InTeam_t inteambll = new Wozuji.BLL.InTeam_t();
            DataSet ds = new DataSet();
            ds = inteambll.GetList(" inTeam_teamId=" + tid.ToString() + " and inteam_userid=" + memberid);
            if (ds.Tables[0].Rows.Count > 0)
            {
                //用户属于本群
                //得到用户群
                Wozuji.BLL.Team_t teambll = new Wozuji.BLL.Team_t();
                return teambll.GetModel(tid);
            }
            else
            {
                return null;
            }



        }
        public Wozuji.Model.Team_t getTeamById(int tid)
        {
            //判断用户是否在这个gruop中
            Wozuji.BLL.InTeam_t inteambll = new Wozuji.BLL.InTeam_t();
            DataSet ds = new DataSet();
            ds = inteambll.GetList(" inTeam_teamId=" + tid.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                //用户属于本群
                //得到用户群
                Wozuji.BLL.Team_t teambll = new Wozuji.BLL.Team_t();
                return teambll.GetModel(tid);
            }
            else
            {
                return null;
            }



        }

        #endregion

        #region 邀请时记录用户要邀请的组群id
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tid"></param>
        public void setInviteGroupId(String tid)
        {

            Wozuji.Common.Cookie.MyCookie ck = new Wozuji.Common.Cookie.MyCookie();
            string oldCookie = "";
            try
            {
                oldCookie = ck.GetCookie("invitegroup").Value;
            }
            catch (Exception ex)
            {

            }

            if (oldCookie != "")
            {

                ck.SetCookie("invitegroup", tid, DateTime.Now.AddDays(30));

            }
            else
            {
                ck.SetCookie("invitegroup", tid, DateTime.Now.AddDays(30));

            }

        }
        #endregion

        #region 移除记录的邀请组群id
        /// <summary>
        /// 移除cookie  invitegroup 
        /// </summary>

        public void removeInviteGroupId()
        {
            Wozuji.Common.Cookie.MyCookie ck = new Wozuji.Common.Cookie.MyCookie();
           
            try
            {
                
                ck.SetCookie("invitegroup", "0", DateTime.Now.AddDays(100));
            }
            catch (Exception ex)
            {

            }

            
        
        }
        #endregion

        #region 从cookie中得到邀请的组群id信息
        public int getInviteGroupId()
        {
            Wozuji.Common.Cookie.MyCookie ck = new Wozuji.Common.Cookie.MyCookie();
            string oldCookie = "";
            try
            {
                oldCookie = ck.GetCookieValue("invitegroup");
            }
            catch (Exception ex)
            {
                oldCookie = "0";
            }
            int gid = 0;
            try
            {
                gid = Convert.ToInt32(oldCookie);
            }

            catch (Exception exx)
            {


            }
            return gid;
        }
        #endregion

        #region 加入组群
        public bool joinGroup(int groupid, int memberid)
        {
            try
            {
                Wozuji.BLL.InTeam_t itbll = new Wozuji.BLL.InTeam_t();
                Wozuji.Model.InTeam_t it = new Wozuji.Model.InTeam_t();
                //判断是否已经加入
                DataSet ds = itbll.GetList(" inteam_teamid=" + groupid + "and inteam_userid=" + memberid);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    removeInviteGroupId();
                    return false;
                }
                else
                {
                    it.InTeam_TeamId = groupid;
                    it.InTeam_UserId = memberid;
                    it.InTeam_Time = DateTime.Now;
                    itbll.Add(it);
                    removeInviteGroupId();
                    return true;
                }
               
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion
    }
}
